/**
*  \file bdi.h
*
*  \brief
*
*  \authors Dajin Li  dajin.li@linde-.de
*
*
*
*/
#ifndef __BDI_H_
#define __BDI_H_

/*================================================[ public includes  ]================================================*/
#include "stm32f10x.h"
/*================================================[ public defines   ]================================================*/

// unsigned int e2read(unsigned int);

//----- Global BDI Constants -----
#define NOMBATT 48 //battery voltage

#define k3                                   3
#define k2                                   3
                                             
#define k0                                   25000000      // ROJ 01.07.04 0.25V
#define k1                                   40000         // 0.0004V

#define QTIME                                15 // data valid period for Qmon

#define Vfull                                ReferenceVoltage * 1000000//211000000           // 2.09V*12 25.32
#define VfullRst                             216000000           // 2.16V*12 25.92

#define BAT_CAL_MAX                          3849     // +4%
#define BAT_CAL_DEF                          3701     // 25.02V cal
#define BAT_CAL_MIN                          3552     // -4%


#if NOMBATT == 48
#define VADCL                                735
#define VADCH                                174
#define MYOFS                                4400    // low calibration point
#elif NOMBATT == 80
#define VADCL                                824
#define VADCH                                224
#define MYOFS                                7000    // low calibration point
#else
#error "NOMBATT = 48V or 80V only !"
#endif
/*================================================[ public datatypes ]================================================*/

/*================================================[ public variables ]================================================*/
//--------------------------------

//--------------------------------

//----- Global BDI Variables -----
extern s8 BDI_BattCharge;     // Battery Charge - State Of Charge reading 0 - 100%
extern s8 BDI_BattCon;        // Battery Condition -  0 - 100%
extern u16 BDI_VBatt ;         // Coarse Battery Reading allow negative volts scale
extern s32 Vin;                 // Scaled voltage in reading used in the calculation
extern u8 BDI_Error;          // error flag - 0 = no error
extern u8 BDI_LowBattFlag;    // Set when BattCharge < 2%
extern u8 BDI_BattCutFlag;    // Set when BattCharge < 1%
extern u8 BDI_FC_Flag;        // Fine/Coarse flag - Set when Fine out of range
extern u8 BDI_LowBattConFlag;// Set when BattCon < 1.94V
extern s32 BattSOC;            // State-Of-Charge modelled voltage
extern s32 BattCon;            // Battery Condition modelled voltage
extern s32 Batt_In;            // Rescaled Battery Input voltage for model
extern s32 last_Batt_In;       // stored for use in BattCon recovery
extern s32 Vempty ;
extern u8  BDI_run;             // flag to start BDI after initalisation
//--------------------------------
extern s32 Volts_Stable_cnt;
extern s32 bdicnt;

//-------- Qmon Stuff --------------
extern s32  Qmax; // current max voltage seen in the last 10s
extern s32  Qmin; // current min voltage seen in the last 10s
extern s32  Qmax_next; // next max voltage seen in the last 10s
extern s32  Qmin_next; // next min voltage seen in the last 10s

extern s32  Qmax_cnt;  // time stamp for current Qmax
extern s32  Qmin_cnt;  // time stamp for current Qmin
extern s32  Qmax_next_cnt; // time stamp for next Qmax
extern s32  Qmin_next_cnt; // time stamp for next Qmin
extern s32  Qmon_cnt;  // Qmon timer
//----------------------------------
extern s16 BDI_vbatt_cal[3];  

//---- Hrs Remaining calculation ----
extern s32 BDI_HrsRem;       // global for holding Estimation of hours remaining as hours x10 i.e. 86=8.6 hours
extern s32 xBattCharge;      // local  previous battery charge value
extern s32 cntBattHrSamples; // count the number of samples so we know when to start using the values - ignore initial one(s) cos they may be inaccurate
extern s32 BattTimer;        // timer for timing between % decreases of capacity
extern u8  BDI_HrsRem_init;   // initialisation flag
extern s16 BDI_vbatt_cal[];    // single calibration point
extern u8  xEXTERNAL_BATTERY ;     // previous state of ExtBatt input
extern u8 BDI_PEDtest;             // test bit to hook into BDI for testing

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
void bdi_init(void );
void bdi_task(void); 
//void BDI_Meas_task(void);         // Function: Voltage measurment - call every 10ms
void HrsRem(int Charge);     // prototype for calculator function - to be called regularly
#endif

/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/


